<%
import ycoa.ui.Renderer
import ycoa.data.classes.ClassFactory 
import ycoa.data.classes.Class

def classFactory = ClassFactory.getInstance()
def renderer = new Renderer()
renderer.setAlignment("top", "center")
def sb = new StringBuilder()
def body = null

if(!params.year) {
  renderer.setTitle("Search")
  sb << """<ul>"""
  def yearList = classFactory.queryUniqueYear()
  yearList.each() { entity ->
    def cls = entity as Class
    sb << """<li><a class="record" href="/classes/${cls.clYear}">${cls.clYear}</a></li>"""
  }
  sb << """</ul>"""
} else if(!params.semester) {
  renderer.setTitle("${params.year}")
  sb << """<h3><a class="record" href="/classes">${params.year}</a></h3>"""
  sb << """<p/>"""
  sb << """<ul>"""
  def semesterList = classFactory.queryUniqueSemester(Integer.parseInt(params.year))
  semesterList.each() { entity ->
    def cls = entity as Class
    sb << """<li><a class="record" href="/classes/${cls.clYear}/${cls.clSemester}">${cls.clSemester}</a></li>"""
  }
  sb << """</ul>"""
} else if(!params.classid) {
  renderer.setTitle("${params.semester} ${params.year}")
  sb << """<p3><a class="record" href="/classes/${params.year}">${params.semester} ${params.year}</a></h3>"""
  sb << """<p/>"""
  sb << """<table width=660 border=0 cellspacing=0 cellpadding=0>
         <tr class="TableHeader">
         <td>&nbsp;</td>
         <td nowrap>&nbsp;<strong>class location</strong>&nbsp;</td>
         <td colspan="2">&nbsp;</td>
         <td nowrap>&nbsp;<strong>weeks</strong>&nbsp;</td>
         <td>&nbsp;</td>
         <td nowrap>&nbsp;<strong>start date</strong>&nbsp;</td>
         <td>&nbsp;</td>
         <td nowrap>&nbsp;<strong>end date</strong>&nbsp;</td>
         <td>&nbsp;</td>
         <td nowrap>&nbsp;<strong>city</strong>&nbsp;</td>
         <td>&nbsp;</td>
         <td nowrap>&nbsp;<strong>sport</strong>&nbsp;</td>
         </tr>
  """
  def toggle = 0
  def classList = classFactory.queryBySession(Integer.parseInt(params.year), params.semester)
  classList.each() { entity ->
    if(toggle==1) { toggle = 0 } else { toggle = 1 }
    def cls = entity as Class
    def address = """${cls.lcAddress},${cls.lcCity},${cls.lcState}"""
    def txtAddress = """${cls.lcAddress} ${cls.lcCity}, ${cls.lcState} ${cls.lcZip}"""
    def mapLink = """http://maps.google.com/maps?f=q&hl=en&geocode=&q=${address}"""
    sb << """
         <tr class="shaded${toggle}">
         <td>&nbsp;&nbsp;</td>
         <td nowrap><a class="record" href="/classes/${cls.clYear}/${cls.clSemester}/${cls.key}">${cls.lcLocation}</a></td>
         <td>&nbsp;&nbsp;&nbsp;</td>
         <td align="right"><a class="record" href="${mapLink}"><font size="-2" color="#3F8640">map</font></a></td>
         <td align=center>14</td>
         <td>&nbsp;</td>
         <td>${renderer.formatDate(cls.clStartDate)}</td>
         <td>&nbsp;</td>
         <td>${renderer.formatDate(cls.clEndDate)}</td>
         <td>&nbsp;</td>
         <td nowrap>${cls.lcCity}</td>
         <td>&nbsp;</td>
         <td>${cls.clSptype}</td>
         </tr>
    """
  }
  sb << """</table>"""
} else if(params.classid) {
  // not possible
  def classList = classFactory.queryByKey(params.classid)
  classList.each() { entity ->
    def cls = entity as Class
  }
} else {
  sb << """<h3>ERROR</h3>"""
}

println renderer.getHeader()
println renderer.getBody(sb.toString())
println renderer.getFooter()

%>
